package com.ht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.domain.bo.TbDoctorBO;
import com.ht.domain.po.*;
import com.ht.mapper.*;
import com.ht.service.TbDoctorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TbDoctorServiceImpl implements TbDoctorService {

    @Autowired
    private TbDoctorMapper tbDoctorMapper;

    @Autowired
    private TbLevelMapper tbLevelMapper;

    @Autowired
    private TbHospitalMapper tbHospitalMapper;

    @Autowired
    private TbDepartmentMapper tbDepartmentMapper;

    @Autowired
    private TbCitiesMapper tbCitiesMapper;

    @Override
    public List<TbDoctorBO> queryDoctor() {
        List<TbDoctor> list = tbDoctorMapper.selectList(null);
        List<TbDoctorBO> listt = new ArrayList<>();
        list.forEach(po->{
            TbDoctorBO bo = new TbDoctorBO();
            //根据医院id查询医院名称
            QueryWrapper<TbHospital> hq = new QueryWrapper<>();
            hq.eq("id",po.getHospitalId());
            TbHospital tbHospital = tbHospitalMapper.selectOne(hq);
            bo.setHospitalName(tbHospital.getHospitalName());
            //根据level查询药师的职位
            QueryWrapper<TbLevel> lq = new QueryWrapper<>();
            lq.eq("id",po.getLevel());
            TbLevel tbLevel = tbLevelMapper.selectOne(lq);
            bo.setLevelName(tbLevel.getLevel());
            //根据科室id查询科室
            QueryWrapper dp = new QueryWrapper();
            dp.eq("id",po.getDepartmentId());
            TbDepartment tbDepartment = tbDepartmentMapper.selectOne(dp);
            bo.setDepartmentName(tbDepartment.getDepartmentName());
            BeanUtils.copyProperties(po,bo);
            listt.add(bo);
        });
        return listt;
    }



    @Override
    public List<TbDoctorBO> queryDoctorByDepartmentIdAndProvinceId(Long provinceId, Long departmentId) {
        List<TbDoctorBO> listt = new ArrayList<>();
        QueryWrapper q = new QueryWrapper();
        q.eq("province_id",provinceId);
        List<TbCities> list = tbDoctorMapper.selectList(q);
        list.forEach(obj->{
            QueryWrapper qq = new QueryWrapper();
            qq.eq("ctiy_id",obj.getId());
            List<TbHospital> list1 = tbHospitalMapper.selectList(qq);
            list1.forEach(objj->{
                QueryWrapper qqq = new QueryWrapper();
                qqq.eq("hospital_id",objj.getId());
                List<TbDoctor> list2 = tbDoctorMapper.selectList(qqq);
                list2.forEach(po->{
                    TbDoctorBO bo = new TbDoctorBO();
                    //根据医院id查询医院名称
                    QueryWrapper<TbHospital> hq = new QueryWrapper<>();
                    hq.eq("id",po.getHospitalId());
                    TbHospital tbHospital = tbHospitalMapper.selectOne(hq);
                    bo.setHospitalName(tbHospital.getHospitalName());
                    //根据level查询药师的职位
                    QueryWrapper<TbLevel> lq = new QueryWrapper<>();
                    lq.eq("id",po.getLevel());
                    TbLevel tbLevel = tbLevelMapper.selectOne(lq);
                    bo.setLevelName(tbLevel.getLevel());
                    //根据科室id查询科室
                    QueryWrapper dp = new QueryWrapper();
                    dp.eq("id",po.getDepartmentId());
                    TbDepartment tbDepartment = tbDepartmentMapper.selectOne(dp);
                    bo.setDepartmentName(tbDepartment.getDepartmentName());
                    BeanUtils.copyProperties(po,bo);
                    listt.add(bo);
                });
            });
        });
        return listt;
    }

    @Override
    public List<TbDoctorBO> queryDoctorPhysical(int i) {
        QueryWrapper q = new QueryWrapper();
        q.eq("inquiry_status",i);
        List<TbDoctor> list = tbDoctorMapper.selectList(q);
        List<TbDoctorBO> listt = new ArrayList<>();
        list.forEach(obj->{
            TbDoctorBO bo = new TbDoctorBO();
            BeanUtils.copyProperties(obj,bo);
            listt.add(bo);
        });
        return listt;
    }
}
